﻿using System;
using System.Collections.Generic;
using System.Data;
using BLL;
using Utils;
namespace UI.ModMini
{
    public partial class CredentialAdd : System.Web.UI.Page
    {
        string id = "";
        string account = "";
        MiniBiz miniBiz = new MiniBiz();
        DefDicBiz defDicBiz = new DefDicBiz();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                User user = Session["user"] as User;
                account = user.Account;
            }
            else
            {
                JSUtility.Alert(this, "您没有登录!请先登录。");
                return;
            }
            if (Request.Params["ID"] != null)
            {
                id = Request.Params["ID"].ToString();
                btnAdd.Enabled = false;
            }
            else
            {
                btnUpdate.Enabled = false;
                btnDelete.Enabled = false;           
               
            }
            if (!IsPostBack)
            {
                defDicBiz.InitDropDownList("Mini_CredentialType", ddlTypeID,"");
                if (id != "")
                {
                    BindData(id);
                }
            }
        }
        private void BindData(string id)
        {
            DataTable dt = miniBiz.GetOneCred(id);
            if (dt.Rows.Count > 0)
            {
                txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                txtCredCode.Text = dt.Rows[0]["CredCode"].ToString();
                txtCredName.Text = dt.Rows[0]["CredName"].ToString();


                defDicBiz.InitDropDownList(ddlTypeID, dt.Rows[0]["TypeID"].ToString());

                txtCredDept.Text = dt.Rows[0]["CredDept"].ToString();
              

                string bTime = dt.Rows[0]["Btime"].ToString();
                if (Utils.RegexRule.IsDate(bTime))
                    txtBtime.Text = Convert.ToDateTime(bTime).ToString("yyyy-MM-dd");
               // else txtBtime.Text = bTime;

                   string eTime = dt.Rows[0]["Etime"].ToString();
                if (Utils.RegexRule.IsDate(eTime))
                    txtEtime.Text = Convert.ToDateTime(eTime).ToString("yyyy-MM-dd");
               // else txtEtime.Text = bTime;

                     string preTime = dt.Rows[0]["PreTime"].ToString();
                if (Utils.RegexRule.IsDate(preTime))
                    txtPreTime.Text = Convert.ToDateTime(preTime).ToString("yyyy-MM-dd");
               // else txtPreTime.Text = preTime;

                     string nextTime = dt.Rows[0]["NextTime"].ToString();
                if (Utils.RegexRule.IsDate(nextTime))
                    txtNextTime.Text = Convert.ToDateTime(nextTime).ToString("yyyy-MM-dd");
                //else txtNextTime.Text = nextTime;

                txtMemo.Text = dt.Rows[0]["Memo"].ToString();
            }

        }
        protected void txtEmpCode_TextChanged(object sender, EventArgs e)
        {
            string empCode = txtEmpCode.Text.Trim();
            if (!string.IsNullOrWhiteSpace(empCode))
            {
                if (Utils.StringUtility.IncludeHZ(empCode))//如果包含汉字
                {
                    DataTable dt = new EmployeeBiz().GetOneInfoByName(empCode);
                    if (dt.Rows.Count == 0)
                    {
                        JSUtility.Alert(this, "不存在该员工姓名: " + empCode);
                    }
                    else if (dt.Rows.Count == 1)
                    {
                        if (dt.Rows[0]["StateName"].ToString() != "离职")
                        {
                            txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                            lbEmpInfo.Text=dt.Rows[0]["EmpName"].ToString() + "  "+dt.Rows[0]["DeptName"].ToString();
                        }
                        else
                            JSUtility.Alert(this, "该员工已经离职: " + empCode);
                    }
                    else
                    {
                        string info = "存在同名，请输入工号：\n\n";
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            info += dt.Rows[i]["EmpCode"].ToString() + "  " + dt.Rows[i]["EmpName"].ToString() + "\n";
                        }
                        JSUtility.Alert(this, info);
                    }
                }
                else
                {
                    DataTable dt = new EmployeeBiz().GetOneInfo(empCode);
                    if (dt.Rows.Count > 0)
                    {
                        if (dt.Rows[0]["StateName"].ToString() != "离职")
                        {
                            lbEmpInfo.Text = dt.Rows[0]["EmpName"].ToString() + "  " + dt.Rows[0]["DeptName"].ToString();
                          }
                        else
                            JSUtility.Alert(this, "该员工已经离职: " + empCode);
                    }
                }
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string credCode = txtCredCode.Text.Trim();
            if (string.IsNullOrWhiteSpace(credCode))
            {
                JSUtility.Alert(this, "编号必须填写");
                return;
            }
           
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("EmpCode", txtEmpCode.Text.Trim());
            sdList.Add("CredCode", credCode);
            sdList.Add("CredName", txtCredName.Text.Trim());
            sdList.Add("TypeID", ddlTypeID.SelectedValue);
            sdList.Add("CredDept", txtCredDept.Text.Trim());

            string bTime = txtBtime.Text.Trim();
            if (bTime != "" && !RegexRule.IsDate(bTime))
            {
                JSUtility.Alert(this, "请输入正确的开始日期");
                return;
            }
            else
            sdList.Add("Btime", bTime);

            string eTime = txtEtime.Text.Trim();
            if (eTime != "" && !RegexRule.IsDate(eTime))
            {
                JSUtility.Alert(this, "请输入正确的失效日期");
                return;
            }
            else
                sdList.Add("Etime", eTime);

            string preTime = txtPreTime.Text.Trim();
            if (preTime != "" && !RegexRule.IsDate(preTime))
            {
                JSUtility.Alert(this, "请输入正确的上次复检日期");
                return;
            }
            else
                sdList.Add("PreTime", preTime);


            string nextTime = txtNextTime.Text.Trim();
            if (nextTime != "" && !RegexRule.IsDate(nextTime))
            {
                JSUtility.Alert(this, "请输入正确的下次复检日期");
                return;
            }
            else
                sdList.Add("NextTime", nextTime);
            sdList.Add("Memo", txtMemo.Text.Trim());
            sdList.Add("Account", account);
            if (miniBiz.Insert("Mini_Credential", sdList))
                JSUtility.Alert(this, "增加成功");
            else
                JSUtility.Alert(this, "增加失败");

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (id == "") return;
            string credCode = txtCredCode.Text.Trim();
            if (string.IsNullOrWhiteSpace(credCode))
            {
                JSUtility.Alert(this, "编号必须填写");
                return;
            }

            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("ID", id);
            sdList.Add("EmpCode", txtEmpCode.Text.Trim());
            sdList.Add("CredCode", credCode);
            sdList.Add("CredName", txtCredName.Text.Trim());
            sdList.Add("TypeID", ddlTypeID.SelectedValue);
            sdList.Add("CredDept", txtCredDept.Text.Trim());

            string bTime = txtBtime.Text.Trim();
            if (bTime != "" && !RegexRule.IsDate(bTime))
            {
                JSUtility.Alert(this, "请输入正确的开始日期");
                return;
            }
            else
                sdList.Add("Btime", bTime);

            string eTime = txtEtime.Text.Trim();
            if (eTime != "" && !RegexRule.IsDate(eTime))
            {
                JSUtility.Alert(this, "请输入正确的失效日期");
                return;
            }
            else
                sdList.Add("Etime", eTime);

            string preTime = txtPreTime.Text.Trim();
            if (preTime != "" && !RegexRule.IsDate(preTime))
            {
                JSUtility.Alert(this, "请输入正确的上次复检日期");
                return;
            }
            else
                sdList.Add("PreTime", preTime);


            string nextTime = txtNextTime.Text.Trim();
            if (nextTime != "" && !RegexRule.IsDate(nextTime))
            {
                JSUtility.Alert(this, "请输入正确的下次复检日期");
                return;
            }
            else
                sdList.Add("NextTime", nextTime);
            sdList.Add("Memo", txtMemo.Text.Trim());
            sdList.Add("Account", account);
            if (miniBiz.Insert("Mini_Credential", sdList))
                JSUtility.Alert(this, "更新成功");
            else
                JSUtility.Alert(this, "更新失败");
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            Response.Redirect(Request.Url.PathAndQuery);
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(id))
            {
                if (miniBiz.DeleteCred(id))
                    JSUtility.Alert(this, "删除成功");
                else
                    JSUtility.Alert(this, "删除失败");
            }
            else
                JSUtility.Alert(this, "没有选择信息");
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            JSUtility.CloseWindow();
        }
    }
}